package com.treasure.mall.biz.service.core.life.impl;

import com.treasure.mall.biz.base.AbstractSearchableCrudService;
import com.treasure.mall.biz.base.SpecificationAndSort;
import com.treasure.mall.biz.dto.BaseSaveDTO;
import com.treasure.mall.biz.dto.BaseSearchDTO;
import com.treasure.mall.biz.dto.condition.BaseSearchCondition;
import com.treasure.mall.biz.entity.life.ShopPO_;
import com.treasure.mall.biz.entity.life.ShopReservationRecordsPO;
import com.treasure.mall.biz.entity.life.ShopReservationRecordsPO_;
import com.treasure.mall.biz.repository.life.ShopReservationRecordsRepository;
import com.treasure.mall.biz.service.core.life.LifeShopService;
import com.treasure.mall.biz.service.core.life.ShopReservationRecordsService;
import com.treasure.mall.biz.service.core.user.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * <p>
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.biz.service.core.life.impl
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/10/13 3:26 下午
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 * 
 */
@Service
public class ShopReservationRecordsServiceImpl extends AbstractSearchableCrudService<ShopReservationRecordsPO, Long> implements ShopReservationRecordsService {

    private ShopReservationRecordsRepository recordsRepository;
    @Autowired
    private UserService userService;
    @Autowired
    private LifeShopService shopService;

    protected ShopReservationRecordsServiceImpl(ShopReservationRecordsRepository repository) {
        super(repository);
        this.recordsRepository = repository;
    }

    @Override
    protected SpecificationAndSort<ShopReservationRecordsPO> specificationAndSort(BaseSearchDTO searchDTO) {
        BaseSearchCondition condition = (BaseSearchCondition) searchDTO;
        Specification<ShopReservationRecordsPO> specification = (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            predicates.add(cb.equal(root.get(ShopReservationRecordsPO_.deleted), false));

            if (StringUtils.isNotBlank(condition.getKeyword())) {
                predicates.add(cb.like(root.get(ShopReservationRecordsPO_.shop).get(ShopPO_.title), likeAll(condition.getKeyword())));
            }
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        };

        Sort sort = new Sort(Sort.Direction.DESC, "id");
        return new SpecificationAndSort<>(specification, sort);
    }

    @Override
    public ShopReservationRecordsPO save(BaseSaveDTO saveDTO) {
        return null;
    }

    /**
     * 添加预约记录
     *
     * @param userId
     * @param shopId
     */
    @Override
    public void insertRecord(Long userId, Long shopId) {
        ShopReservationRecordsPO po = new ShopReservationRecordsPO();
        po.setUser(userService.findById(userId));
        po.setShop(shopService.findById(shopId));
        po.setCreateTime(LocalDateTime.now());
        save(po);
    }
}
